package chat;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Color;

import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.Rectangle;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChatingClient extends JPanel implements Runnable{
	
	
	
	public  JTextArea textArea;
	


	private JTextField txtId;
	private JTextField txtPwd;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField txtPort;
	private JTextField textField_5;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Create the panel.
	 */
	public ChatingClient() {
		setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 660, 33);
		add(panel);
		
		textField_5 = new JTextField();
		textField_5.setText("IP");
		textField_5.setHorizontalAlignment(SwingConstants.CENTER);
		textField_5.setColumns(10);
		textField_5.setBackground(Color.GREEN);
		panel.add(textField_5);
		
		txtPort = new JTextField();
		txtPort.setText("7777");
		txtPort.setHorizontalAlignment(SwingConstants.CENTER);
		txtPort.setColumns(10);
		txtPort.setBackground(Color.GREEN);
		panel.add(txtPort);
		
		txtId = new JTextField();
		txtId.setText("ID");
		txtId.setHorizontalAlignment(SwingConstants.CENTER);
		txtId.setColumns(10);
		txtId.setBackground(Color.GREEN);
		panel.add(txtId);
		
		txtPwd = new JTextField();
		txtPwd.setText("Pwd");
		txtPwd.setHorizontalAlignment(SwingConstants.CENTER);
		txtPwd.setColumns(10);
		txtPwd.setBackground(Color.GREEN);
		panel.add(txtPwd);
		
		JButton button = new JButton("로그인");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Thread t = new Thread(ChatingClient.this);
				t.start();
			}
		});
		panel.add(button);
		
		JButton button_1 = new JButton("로그아웃");
		panel.add(button_1);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(0, 346, 660, 23);
		add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		textField_3 = new JTextField();
		textField_3.setText("이모티콘");
		textField_3.setHorizontalAlignment(SwingConstants.CENTER);
		textField_3.setColumns(10);
		textField_3.setBackground(Color.GREEN);
		panel_2.add(textField_3, BorderLayout.WEST);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(new Rectangle(0, 0, 500, 0));
		panel_2.add(textField_2);
		
		JButton button_2 = new JButton("전송");
		panel_2.add(button_2, BorderLayout.EAST);
		
		textField = new JTextField();
		textField.setBounds(0, 33, 233, 303);
		add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(245, 31, 345, 305);
		add(textField_1);
		textField_1.setColumns(10);

	}

	@Override
	public void run() {
		try{
			int port = Integer.parseInt(txtPort.getText());
			Socket s = new Socket(txtPort.getText(),port);
			int len = textArea.getText().length();
			textArea.setCaretPosition(len);		
			//
			System.out.println(port);
			System.out.println(txtPort.getText());
			//
			
			
			OutputStream os= s.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			
			InputStream is = s.getInputStream();
			ObjectInputStream ois = new ObjectInputStream(is);
			
			chatData cd= (chatData)ois.readObject();
			textField_1.setText(cd.getMessage()+ "\n");
			
			//
			System.out.println(cd.getMessage());
			
					
		}catch(Exception ex){
			
		}
		
		
		
	}
}
